import util from "../../common/js/util.js";
var app = getApp();
Component({
  mixins: [],
  data: {
    staffNum: 0,
    indexArry: [],//带大写字母分组的 人员 有排序
    staff: [],
    staffArry: null,
    letterMap: new Map(),
    action: "",
    alphabet: [],
    toView: "",
    show: false,
  },
  props: {
    multiple: true,
    checkStaffList: "",//英文逗号隔开的字符串
    disStaffList:"",
    onPass: function onPass(e) { }
  },
  didMount() {
    //console.log(app);
    const charCode = 65;
    const charList = [];
    let checkStaffList = [];
    if (util.isNotEmpty(this.props.checkStaffList)) {
      try {
        checkStaffList = this.props.checkStaffList.split(",");
      } catch (error) {

      }
    }
    for (let i = 0; i < 26; i++) {
      charList.push(String.fromCharCode(charCode + i));
    }
    this.setData({
      alphabet: charList,
    });
    this.staff = [];
    if (util.isNotEmpty(app.prototype.$indexObj)) {
      this.initSortArr(app.prototype.$indexObj, this)
    } else {
      let promise=app.getUserNameMapFn({}, app);
      let that = this;
      promise.then(
        res => {
          that.initSortArr(res.$indexObj, that)
        });
      // setTimeout(e => {
      //   that.initSortArr(app.prototype.$indexObj, that)
      // }, 500);
    }


  },
  didUpdate() {
    this.setData({
      multiple: this.props.multiple
    })
  },
  didUnmount() { },
  methods: {
    checkboxRowClick(e){
      let username = e.currentTarget.dataset.username;
      let letterIndex = e.currentTarget.dataset.letterIndex;
      let index = e.currentTarget.dataset.index;
      let checked = e.currentTarget.dataset.checked;
      let disabled = e.currentTarget.dataset.disabled;
      let result=!checked;
      let staff=this.data.staff;
     // console.log(disabled);
      if(!disabled){
        if(result){
          staff.push(username)
        }else{
         staff= staff.filter(e=>{
            return e!=username
          })
        }
        this.setData({
          [`indexArry[${letterIndex}].data[${index}].checked`]:result,
          staff
        })
      }
      
      
    },
    initSortArr(list, that) {
      let indexArry = [], staffNum = 0;
      for (let i in list) {
        if (i != "#") {
          let node = list[i];
          staffNum += node.data.length;
          indexArry.push(node);
        }
      };
      indexArry.sort((a, b) => a.letter.charCodeAt(0) - b.letter.charCodeAt(0)); // 排序
      let letterMap = new Map();
      letterMap.set("#", indexArry.length);
      that.setData({
        indexArry, letterMap, staffNum
      })

    //  console.log(indexArry);
    },
    openInitSortArr(list){
      this.initSortArr(list,this);
      if(this.data.indexArry.length>0){
        util.message("联系人初始化成功!");
      }
    },
    //打开有勾选的联系人
    openFn(strs) {
      //  console.log(this.props.checkStaffList);

      this.setData({
        show: true
      })
     // let checkStaffList=this.props.checkStaffList;
      let checkStaffList=[];
      this.checkedAllNo();
      let disStaffList=this.props.disStaffList;

      
      if(util.isNotEmpty(disStaffList)){
        let list=disStaffList.split(",");
        this.initDisStaffList(list)
      }

      if(util.isEmpty(checkStaffList)&&util.isNotEmpty(strs)){
        let list=[];
        if(strs instanceof String){
          list=strs.split(",")
        }else{
          list=strs;
        }
        
        checkStaffList=list;
      }
       if (util.isNotEmpty(checkStaffList)) {
        if((typeof checkStaffList=='string')&&checkStaffList.constructor==String){
           checkStaffList = checkStaffList.split(",");
        }
        
      }
      if (util.isNotEmpty(checkStaffList)) {
        this.initCheckStaffList(checkStaffList);
      }

    },
    //打开 禁用勾选的联系人
    openDisFn(disStaffList){
      this.checkedAllNo();
      if(util.isNotEmpty(disStaffList)){
        disStaffList=disStaffList+","+app.prototype.$SysUserName;
        let list=disStaffList.split(",");
        this.initDisStaffList(list)
      }else{
        disStaffList=app.prototype.$SysUserName;
        let list=disStaffList.split(",");
        this.initDisStaffList(list)
      }
      this.setData({
        show: true
      })
    },
    //打开一个默认没有勾选的联系人
    openEmpty(){
      this.setData({
        show: true
      })
    },
    onPass() {
      this.props.onPass(this.data.staff);
      this.onCloseFn();
    },
    initCheckStaffList(checkStaffList) {
      let staff = [], indexArry = this.data.indexArry;
      for (let i in indexArry) {
        let node = indexArry[i];
        if (util.isNotEmpty(checkStaffList)) {
          for (let index = 0; index < node.data.length; index++) {
            let userName = node.data[index].userName;
            for (let i = 0; i < checkStaffList.length; i++) {
              const element = checkStaffList[i];
              if (userName == element) {
                node.data[index].checked = true;
                staff.push(userName);
              }
            }
          }
        }
      };
      this.setData({
        staff, indexArry
      })
      console.log(staff);

    },
    initDisStaffList(disStaffList) {
      let staff = [], indexArry = this.data.indexArry;
      for (let i in indexArry) {
        let node = indexArry[i];
        if (util.isNotEmpty(disStaffList)) {
          for (let index = 0; index < node.data.length; index++) {
            let userName = node.data[index].userName;
            for (let i = 0; i < disStaffList.length; i++) {
              const element = disStaffList[i];
              if (userName == element) {
                node.data[index].disabled = true;
               // staff.push(userName);
              }
            }
          }
        }
      };
      this.setData({
        indexArry
      })
     // console.log("----indexArry",indexArry);

    },
    onCloseFn() {
      this.setData({
        show: false
      });
      // console.log(this.data.show);

      this.checkedAllNo();
    },
    pass() {
      if (this.data.staff.length == 0) {
        return false;
      }
      // console.log(this.data.staff);
      this.onPass();
    },
    onAlphabetClick(ev) {
      let id = ev.data.item;
      this.setData({
        toView: id
      })
    },
    checkboxChange(e) {
      if (this.props.multiple) {
        //debugger
        //多选 模式
        this.checkedOneClickFn(e);
      } else {
        //单选
        this.checkboxRowClick(e);
        //this.checkedOneMultiple(e);
      //  console.log(this.data.indexArry);
      }
    },
    checkedOneMultiple(e) {
      let checked = e.detail.value;
      let username = e.currentTarget.dataset.username;
      let indexArry = this.data.indexArry;
      let staff = [];
      indexArry.forEach(element => {
        element.data.forEach(item => {
          // debugger
          if (item.userName == username) {
            item.checked = checked;
            if (checked) {
              staff.push(item.userName);
            }
          } else {
            item.checked = false;
          }
        })
      });
      // console.log(indexArry);
      this.setData({
        indexArry, staff
      })
    },
    checkedOneClickFn(e) {
      let checked = e.detail.value;
      let username = e.currentTarget.dataset.username;
      // staff
      if (checked) {
        this.addItem(username)
      } else {
        this.removeItem(username);
      }
    },
    checkedAllFn(e) {
      let checked = e.detail.value;
      if (checked) {
        this.checkedAllYes();
      } else {
        this.checkedAllNo();
      }
    },
    checkedAllYes() {
      let staff = [];
      let indexArry = this.data.indexArry;
      indexArry.forEach(element => {
        element.data.forEach(item => {
          item.checked = true;
          staff.push(item.userName);
        })
      });
      this.setData({
        indexArry, staff
      })
    },
    checkedAllNo() {
      let indexArry = this.data.indexArry;
      let staff = [];
      indexArry.forEach(element => {
        element.data.forEach(item => {
          item.checked = false;
          item.disabled = false;
        
        })
      });


      this.setData({
        indexArry, staff
      })
    },
    addItem(e) {
      let staff = this.data.staff;
      let find = staff.find(item => {
        return e == item;
      });
      // console.log(find);
      if (!find) {
        staff.push(e);
      }
      this.setData({
        staff
      })
    },
    removeItem(e) {
      let staff = this.data.staff;
      let new_arr = staff.filter(item => {
        return item != e;
      })
      this.setData({
        staff: new_arr
      })
    },
  }

});
